<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="nl">
  <head>
    <link rel="stylesheet" type="text/css" href="sfp.css" />

    <script type="text/javascript" src="external.js"></script>
    <script type="text/javascript" src="home.js"></script>

    <title>SAX Delphi XML Pascal</title>
    <meta name="Author" content="Sax for Pascal @ SourceForge.net" />
    <meta name="Keywords" content="SAX,Delphi,XML,Pascal" />
    <meta name="Description" content="SAX for Pascal is the Delphi port of the Simple API for XML Parsing interfaces." />
    <meta name="robots" content="index,follow" />
  </head>
  <body>
    <script type="text/javascript">
      <!--
      // If not loaded in frames, load the frames
      if (window.name == "") {
        location.reload ("index.html?saxdom.html")
      }
      // -->
    </script>
    <h1>SAX vs. DOM</h1>
    <p>
      SAX (<a rel="external" href="http://www.saxproject.org/">Simple API for XML</a>) and DOM 
      (<a rel="external" href="http://www.w3.org/DOM/">Document Object Model</a>) were both 
      designed to allow programmers to access their information without having to write a parser. 
      By keeping the information in XML format, and by using either SAX or DOM 
      <b class="sc">api</b>s your program is free to use whatever parser it wishes. This 
      can happen because parser writers must implement the SAX and DOM 
      <b class="sc">api</b>s.
    </p>
		<p>
		  So both SAX and DOM were created to serve the same purpose, which is giving you access to the 
		  information stored in XML documents. However, both of them take very different approaches to 
		  giving you access to your information.
    </p>
    
    <h2>DOM</h2>
    <p>
      DOM gives you access to the information stored in your XML document as a hierarchical object 
      model. DOM creates a tree of nodes (based on the structure and information in your XML 
      document) and you can access your information by interacting with this tree of nodes. This 
      works out really well because XML is hierarchical in nature. 
    </p>
    <p>
      Delphi facilitates use of a DOM by it's <code>TXMLDocument</code> component (available in
      the Enterprise version of Delphi 6, or in the Professional version of Delphi 7) and
      by the XML Data Binding wizard (available in the Enterprise versions of Delphi 6 and 7).
    </p>
    
    <h2>SAX</h2>
    <p>
      SAX chooses to give you access to the information in your XML document, not as a tree of nodes,
      but as a sequence of events! You ask, how is this useful? The answer is that SAX chooses not to 
      create a default Delphi object model on top of your XML document (like DOM does). This 
      makes SAX faster, but also necessitates the following things:
    </p>
    <ul>
      <li>Creation of your own custom object model;</li>
      <li>creation of a class that listens to SAX events and properly creates your object model.</li>
    </ul>
    <p>
      Note that these steps are not necessary with DOM, because DOM already creates an object model 
      for you (which represents your information as a tree of nodes).
    </p>
    <p>
      In the case of DOM, the parser does almost everything: read the XML document in, create an 
      object model on top of it and then give you a reference to this object model (a 
      <code>Document</code> object) so that you can manipulate it. 
      SAX is not called the Simple API for XML for nothing, it is really simple. 
      SAX doesn't expect the parser to do much, all SAX requires is that the parser 
      should read in the XML document, and fire a bunch of events depending on what tags it 
      encounters in the XML document. You are responsible for interpreting these events by writing an 
      XML document handler class, which is responsible for making sense of all the tag events and 
      creating objects in your own object model.
    </p>
    <p>
      What kinds of SAX events are fired by the SAX parser? These events are really very simple. SAX 
      will fire an event for every open tag, and every close tag. It also fires events for 
      <b class="sc">#pcdata</b> and for <b class="sc">cdata</b> sections. Your document 
      handler (which is a listener for these events) has to interpret these events in some 
      meaningful way and create your custom object model based on them. 
      Your document handler will have to interpret these events, and the sequence in which these 
      events are fired is very important. SAX also fires events for processing instructions, DTDs, 
      comments, etc.
    </p>
    <p>
      Delphi by default does not provide any support for SAX. This is why the 
      <b class="name">SAX for Pascal</b> project was initiated.
    </p>
    
    <h2>SAX vs. DOM</h2>
    <p>
      Both SAX and DOM have their respective strengths and weaknesses, as the following table
      shows:
    </p>
    <table>
      <tr><td class="empty"/><td class="header">SAX</td><td class="header">DOM</td></tr>
      <tr>
        <td class="rowheader">Advantages</td>
        <td>
          <ul>
            <li>Speed</li>
            <li>Can process arbitrarily large documents due to low memory requirements</li>
          </ul>
        </td>
        <td>
          <ul>
            <li>Arbitrary navigability</li>
            <li>Makes XSL-style transformations much easier</li>
          </ul>
        </td>
      </tr>
      <tr>
        <td class="rowheader">Disadvantages</td>
        <td>
          <ul>
            <li>No navigability</li>
            <li>Event based programming hard for some programmers</li>
            <li>Need to maintain state externally (makes XSL-style transformations hard to code)</li>
          </ul>
        </td>
        <td>
          <ul>
            <li>Uses lots of memory, not well suited for large documents</li>
            <li>Slow, as it needs to build an in-memory representation</li>
            <li>Tight coupling between XML document and object model</li>
          </ul>
        </td>
      </tr>
    </table>
    <p>
      The SAX document handler you write does element to object mapping. So, if your information is 
      structured in a way that makes it easy to create this mapping, you should use the SAX 
      <b class="sc">api</b>. 
      On the other hand, if your data is much better represented as a tree then you should use DOM.
    </p>
    
    <h2>Other references</h2>
    <p>
      <a rel="external" href="http://www.xml.com/pub/rg/SAX_vs_DOM"><code>xml.com</code></a>
      contains more links to articles about the differences and advantages of SAX and DOM.
    </p>

    <hr />
    <div><a href="javascript:home()">Home</a></div>
    
  </body>
</html>
